package principal;



import analyzer.AnalyzerAgent;
import carGeneration.CarGeneratorAgent;
import lightControl.LightControlAgent;
import org.objectweb.proactive.Body;
import org.objectweb.proactive.ProActive;
import org.objectweb.proactive.core.config.ProActiveConfiguration;
import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
import org.objectweb.proactive.core.descriptor.data.VirtualNode;
import org.objectweb.proactive.core.util.CircularArrayList;
import org.objectweb.proactive.ext.migration.MigrationStrategyManagerImpl;


public class SimulatorControler implements org.objectweb.proactive.RunActive,
    AgentMessageReceiver, java.io.Serializable {
    //The image panel
    private transient simulatorApplet display;
    protected CircularArrayList streetAgentList;
    String[] args;
    private MigrationStrategyManagerImpl myStrategyManager;
    private CarGeneratorAgent myCarGeneratorAgent;
    private LightControlAgent myLightControlAgent;
    private StreetControllerAgent myStreetControllerAgent;
    private StreetControllerAgent myStreetControllerAgent_2;
    
    public SimulatorControler() {
    }

    public SimulatorControler(String[] args) {
        this.streetAgentList = new CircularArrayList(20);
        this.args = args;
        //lo siguiente es para probar las estructuras pasivas.
     
        try {
            ProActive.turnActive(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void rebuild() {
        this.display = new simulatorApplet((SimulatorControler) ProActive.getStubOnThis(),
                streetAgentList);
    }

    public void clean() {
        if (display != null) {
            display.dispose();
            display = null;
        }
    }

    public void receiveMessage(String s) {
        display.receiveMessage(s);
         if(s.equals("0")){
           System.out.println("start lightControler");
           this.myLightControlAgent.start(this.myStreetControllerAgent);
        }
        if(s.equals("1")){
           System.out.println("Finish lightControler");
           this.myCarGeneratorAgent.start(this.myStreetControllerAgent);
        }
        if(s.equals("2")){
            System.out.println("Finish carGenerator");
            this.myStreetControllerAgent.start();
        }
        display.receiveMessage("End Receiving message");
    }

    public void receiveMessage(String s, java.awt.Color c) {
        display.receiveMessage(s, c);
    }

    public StreetControllerAgent createStreetAgent(int n) {
        try {
            StreetControllerAgent newStreetAgent = (StreetControllerAgent) org.objectweb.proactive.ProActive.newActive(StreetControllerAgent.class.getName(),
                    new Object[] { new Integer(n) });
            newStreetAgent.initialize(args);
            newStreetAgent.setControler((SimulatorControler) ProActive.getStubOnThis());
            myStreetControllerAgent = newStreetAgent;
            return newStreetAgent;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void runActivity(Body b) {
        org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(b);
        myStrategyManager = new MigrationStrategyManagerImpl((org.objectweb.proactive.core.body.migration.Migratable) b);
        myStrategyManager.onDeparture("clean");
        rebuild();
        service.fifoServing();
        clean();
    }

    //<<<<<<< SimulatorControler.java
    public static void main(String[] args) {
        ProActiveConfiguration.load();
        // Version without descriptor
        //    try {
        //      // ProActive.newActive(AdvancedPenguinControler.class.getName(),null,(Node) null);
        //      new SimulatorControler(args);
        //    } catch (Exception e) {
        //      e.printStackTrace();
        //    }
        // Version with descriptor
        ProActiveDescriptor proActiveDescriptor = null;
        try {
            proActiveDescriptor = ProActive.getProactiveDescriptor("file:" +
                    args[0]);
            proActiveDescriptor.activateMappings();
            VirtualNode vn1 = proActiveDescriptor.getVirtualNode("TrafficNode");

            //Thread.sleep(15000);
            String[] nodes = vn1.getNodesURL();
            new SimulatorControler(nodes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public    CarGeneratorAgent createCarGeneratorAgent(int n) {
      
        try {
           CarGeneratorAgent newCarGeneratorAgent = (CarGeneratorAgent) org.objectweb.proactive.ProActive.newActive(CarGeneratorAgent.class.getName(),
                    new Object[] { new Integer(n) });
            newCarGeneratorAgent.initialize(args);
            newCarGeneratorAgent.setControler((SimulatorControler) ProActive.getStubOnThis());
            myCarGeneratorAgent = newCarGeneratorAgent;
            return newCarGeneratorAgent;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

 public   LightControlAgent createLightControlAgent(int n) {
            try {
           LightControlAgent newLightControlAgent = (LightControlAgent) org.objectweb.proactive.ProActive.newActive(LightControlAgent.class.getName(),
                    new Object[] { new Integer(n) });
            newLightControlAgent.initialize(args);
            newLightControlAgent.setControler((SimulatorControler) ProActive.getStubOnThis());
            myLightControlAgent = newLightControlAgent;
            return newLightControlAgent;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

 public   AnalyzerAgent createAnalyzerAgent(int n) {
      try {
           AnalyzerAgent newAnalyzerAgent = (AnalyzerAgent) org.objectweb.proactive.ProActive.newActive(AnalyzerAgent.class.getName(),
                    new Object[] { new Integer(n) });
            newAnalyzerAgent.initialize(args);
            newAnalyzerAgent.setControler((SimulatorControler) ProActive.getStubOnThis());
            return newAnalyzerAgent;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

 
}
